/**
 * Created by wushanxin on 14-4-8.
 */

var log = require("../lib/log");
var config = require('../config');
var crypto = require('crypto');
var RUtil = require('./dataformatUtil');


var db = require("../lib/db");
var roles=require('../models/roles.js');
exports.index = function(request, response) {

    var method = request.method || '';

    if(typeof(request.session.admin)=="undefined" || request.session.admin==null || request.session.admin.length==0)
    {
        response.send("<script>window.parent.location.href='login.html';</script>");
        return;
    }

    if(request.session.roleinfo.flag!==99)
    {
        response.send("<script>window.parent.location.href='index.html';</script>");
        return;
    }

    var rolesdata=RUtil.rolesdata(request);
    rolesdata.retdata.nickname =request.session.admin.nickname;
    rolesdata.retdata.roleinfo = request.session.roleinfo;
    rolesdata.retdata.userinfo = request.session.admin;
    rolesdata.retdata.rolelist={};
    rolesdata.retdata.title="权限管理";
    if(method.toUpperCase() === "GET") {
        rolesdata.retdata.authlistemun=RUtil.authlistemun;
        rolesdata.retdata.statlistemun=RUtil.statlistemun;

        roles.selectAll(function(error,results) {
            if((error)!== null)
            {
                // 数据库异常
                rolesdata.retdata.info = "载入失败：系统内部错误(错误码："+error.errno+")";
                rolesdata.retdata.success=false;

                response.render("userinfo", rolesdata);

            }
            else if(results.length <= 0) {
                // 不存在，直接响应页面
                rolesdata.retdata.info = "没有任何权限分级记录!";
                rolesdata.retdata.success=false;

                response.render("userinfo", rolesdata);
            } else {
                //存在数据将数据发到页面
                rolesdata.retdata.success=true;
                for(var i = 0; i < results.length; i++)
                {
                    results[i].authnumber=RUtil.getspecialauthlist(results[i].authnumber);
                    results[i].statnumber=RUtil.getspecialstatlist(results[i].statnumber);
                    results[i].childroleid=results[i].childroleid.split(",");
                }

                rolesdata.retdata.rolelist= results;


                if(request.query.oper == "add")
                {
                    rolesdata.retdata.info = "添加权限分级信息";
                    rolesdata.retdata.success=true;

                    response.render("roleinfo.html",rolesdata);
                }
                else if(request.query.oper == "edit")
                {
                    roles.selectByID(request.query.id, function(error,results) {
                        if((error)!== null)
                        {
                            // 数据库异常
                            rolesdata.retdata.info = "载入失败：系统内部错误(错误码："+error.errno+")";
                            rolesdata.retdata.success=false;

                            response.render("roleinfo", rolesdata);

                        }
                        else if(results.length <= 0) {
                            // 不存在，直接响应页面
                            rolesdata.retdata.info = "没有任何权限分级记录!";
                            rolesdata.retdata.success=false;

                            response.render("roleinfo", rolesdata);
                        } else {
                            //存在数据将数据发到页面

                            rolesdata.retdata.info = "编辑权限分级记录";
                            rolesdata.retdata.success=true;
                            rolesdata.role= results[0];

                            response.render("roleinfo", rolesdata);
                        }
                    });
                }

            }
        });

    }

    else if(method.toUpperCase() === "POST") {
        if(request.body.oper == "add")
        {
            var role = RUtil.role(request);
            role.rolename=request.body.rolename;
            role.wifinumber=request.body.wifinumber;
            role.apnumber=request.body.apnumber;
            role.authnumber=RUtil.getrolenum(request.body.authnumber);
            role.statnumber=RUtil.getrolenum(request.body.statnumber);
            role.childroleid=RUtil.getchildroleid(request.body.childroleid);

            role.usernumber=request.body.usernumber;
            role.adnumber=request.body.adnumber;
            role.fee=request.body.fee;
            role.descs=request.body.descs;
            role.flag=request.body.flag;
            role.validmonth=request.body.validmonth;

            roles.insert(role, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    var info = { status:"error",info:'添加失败：系统内部错误(错误码：'+error.errno+')' };
                    if(error.errno==1062)
                        info = { status:"error",info:'添加失败：权限名已经存在！)' };
                    response.send((info));
                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    var info = { status:"error",info:'添加失败！' };
                    response.send( (info));
                } else {
                    //存在数据将数据发到页面
                    var info = { status:"success",info:'添加成功！' };
                    response.send( (info));
                }
            });
        }
        else if(request.body.oper == "edit")
        {

            var role = RUtil.role(request);
            role.id=request.body.id;
            role.rolename=request.body.rolename;
            role.wifinumber=request.body.wifinumber;
            role.apnumber=request.body.apnumber;
            role.authnumber=RUtil.getrolenum(request.body.authnumber);
            role.statnumber=RUtil.getrolenum(request.body.statnumber);
            role.childroleid=RUtil.getchildroleid(request.body.childroleid);
            role.usernumber=request.body.usernumber;
            role.adnumber=request.body.adnumber;
            role.fee=request.body.fee;
            role.descs=request.body.descs;
            role.flag=request.body.flag;
            role.validmonth=request.body.validmonth;

            roles.update(role, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    var info = { status:"error",info:'更新失败：系统内部错误(错误码：'+error.errno+')' };
                    if(error.errno==1062)
                        info = { status:"error",info:'更新失败：权限名已经存在！)' };
                    response.send((info));
                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    var info = { status:"error",info:'更新失败！' };
                    response.send( (info));
                } else {
                    //存在数据将数据发到页面
                    var info = { status:"success",info:'更新成功！' };
                    response.send( (info));
                }
            });
        }
    }

};


